package com.ronsai.umtrack.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.ronsai.umtrack.R;
import com.ronsai.umtrack.adapter.HomeAdapter;
import com.ronsai.umtrack.base.BaseFragment;
import com.ronsai.umtrack.contract.HomeContract;
import com.ronsai.umtrack.presenter.HomePresenter;
import com.ronsai.umtrack.util.GlideImageLoader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.ObservableTransformer;

public class HomeFragment extends BaseFragment<HomeContract.View, HomeContract.Presenter> implements HomeContract.View {

    private HomeAdapter homeAdapter;
    private List<String> list = new ArrayList<>();
    private boolean isLoadMore = false;
    private int page = 1;
    private int pageSize = 20;
    private Banner banner;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected HomeContract.Presenter createPresenter() {
        return new HomePresenter(getActivity());
    }

    @Override
    protected HomeContract.View createView() {
        return this;
    }

    @Override
    protected void init(View rootView, @Nullable Bundle savedInstanceState) {
        RefreshLayout refreshLayout = rootView.findViewById(R.id.refreshLayout);
        refreshLayout.setRefreshHeader(new ClassicsHeader(getActivity()));
        refreshLayout.setRefreshFooter(new ClassicsFooter(getActivity()));
        refreshLayout.setOnRefreshListener(refreshlayout -> {
            refreshLayout.getLayout().postDelayed(() -> {
                isLoadMore = false;
                page = 1;
                loadData();
                refreshLayout.finishRefresh();
            }, 1000);
        });
        refreshLayout.setOnLoadMoreListener(refreshlayout -> {
            refreshLayout.getLayout().postDelayed(() -> {
                isLoadMore = true;
                page++;
                loadData();
                refreshLayout.finishLoadMore();
            }, 1000);
        });
        RecyclerView recyclerView = rootView.findViewById(R.id.recyclerView);
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(),2);
//        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
//        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        homeAdapter = new HomeAdapter(R.layout.item_home, list);
        recyclerView.setAdapter(homeAdapter);
        loadData();

        // banner
        banner = (Banner) LayoutInflater.from(getActivity()).inflate(R.layout.item_home_banner, recyclerView, false);
        banner.setIndicatorGravity(BannerConfig.RIGHT);
        banner.setImageLoader(new GlideImageLoader());
        //getPresenter().getHomeBannerList();
        List<com.ronsai.umtrack.entity.Banner> bannerList = new ArrayList<>();
        bannerList.add(new com.ronsai.umtrack.entity.Banner(1, "/images/app/banner/banner2.png", 1, 1));
        bannerList.add(new com.ronsai.umtrack.entity.Banner(2, "/images/app/banner/banner3.png", 2, 2));
        bannerList.add(new com.ronsai.umtrack.entity.Banner(3, "/images/app/banner/banner5.png", 3, 0));
        bannerList.add(new com.ronsai.umtrack.entity.Banner(4, "/images/app/banner/banner2.png", 4, 0));
        banner.setImages(bannerList);
        banner.start();
        banner.setOnBannerListener(position -> Log.d("log_test", "position: " + position));
        homeAdapter.addHeaderView(banner);
        homeAdapter.openLoadAnimation();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        banner.stopAutoPlay();
    }

    private void loadData() {
        if (!isLoadMore) list.clear();
        for (int i = (page - 1) * pageSize; i < page * pageSize; i++) {
            list.add("item" + i);
        }
        homeAdapter.notifyDataSetChanged();
    }

    @Override
    public void getBannerSuccess(List<com.ronsai.umtrack.entity.Banner> list) {
        banner.setImages(list);
        banner.start();
    }

    @Override
    public <T> ObservableTransformer<T, T> bindLifecycle() {
        return bindToLifecycle();
    }
}
